package com.sangcomz.fishbun;

import com.sangcomz.fishbun.ui.picker.PickerActivity;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.element.Element;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import ohos.utils.net.Uri;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by sangcomz on 17/05/2017.
 */
public final class FishBunCreator implements BaseProperty, CustomizationProperty {
    private int requestCode = FishBun.FISHBUN_REQUEST_CODE;
    private FishBun fishBun;
    private Fishton fishton;

    public FishBunCreator(FishBun fishBun, Fishton fishton) {
        this.fishton = fishton;
        this.fishBun = fishBun;
    }

    @Override
    public FishBunCreator setSelectedImages(ArrayList<Uri> selectedImages) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        fishton.selectedImages.addAll(selectedImages);
        return this;
    }

    @Override
    public FishBunCreator setAlbumThumbnailSize(int size) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        fishton.albumThumbnailSize = size;
        return this;
    }

    @Override
    public FishBunCreator setPickerSpanCount(int spanCount) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        if (spanCount <= 0) {
            fishton.photoSpanCount = 3;
        } else {
            fishton.photoSpanCount = spanCount;
        }
        return this;
    }

    @Override
    public FishBunCreator setPickerCount(int count) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        setMaxCount(count);
        return this;
    }

    @Override
    public FishBunCreator setMaxCount(int count) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        if (count <= 0) {
            fishton.maxCount = 1;
        } else {
            fishton.maxCount = count;
        }
        return this;
    }


    @Override
    public FishBunCreator setMinCount(int count) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        if (count <= 0) {
            fishton.minCount = 1;
        } else {
            fishton.minCount = count;
        }
        return this;
    }

    @Override
    public FishBunCreator setActionBarTitleColor(int actionBarTitleColor) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        fishton.colorActionBarTitle = actionBarTitleColor;
        return this;
    }

    @Override
    public FishBunCreator setActionBarColor(int actionBarColor) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        fishton.colorActionBarTitle = actionBarColor;
        return this;
    }

    @Override
    public FishBunCreator setActionBarColor(int actionBarColor, int statusBarColor) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        fishton.colorActionBar = actionBarColor;
        fishton.colorStatusBar = statusBarColor;
        return this;
    }

    @Override
    public FishBunCreator setActionBarColor(int actionBarColor, int statusBarColor, boolean isStatusBarLight) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        fishton.colorActionBar = actionBarColor;
        fishton.colorStatusBar = statusBarColor;
        fishton.isStatusBarLight = isStatusBarLight;
        return this;
    }

    @Override
    public FishBunCreator setCamera(boolean isCamera) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        fishton.hasCameraInPickerPage = isCamera;
        return this;
    }

    @Override
    public FishBunCreator hasCameraInPickerPage(boolean hasCamera) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        fishton.hasCameraInPickerPage = hasCamera;
        return this;
    }

    @Override
    public FishBunCreator setRequestCode(int requestCode) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        this.requestCode = requestCode;
        return this;
    }

    @Override
    public FishBunCreator textOnNothingSelected(String message) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        fishton.messageNothingSelected = message;
        return this;
    }

    @Override
    public FishBunCreator textOnImagesSelectionLimitReached(String message) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        fishton.messageLimitReached = message;
        return this;
    }

    @Override
    public FishBunCreator setButtonInAlbumActivity(boolean isButton) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        fishton.hasButtonInAlbumActivity = isButton;
        return this;
    }

    @Override
    public FishBunCreator setReachLimitAutomaticClose(boolean isAutomaticClose) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        fishton.isAutomaticClose = isAutomaticClose;
        return this;
    }

    @Override
    public FishBunCreator setAlbumSpanCount(int portraitSpanCount, int landscapeSpanCount) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        fishton.albumPortraitSpanCount = portraitSpanCount;
        fishton.albumLandscapeSpanCount = landscapeSpanCount;
        return this;
    }

    @Override
    public FishBunCreator setAlbumSpanCountOnlyLandscape(int landscapeSpanCount) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        fishton.albumLandscapeSpanCount = landscapeSpanCount;
        return this;
    }

    @Override
    public FishBunCreator setAlbumSpanCountOnlPortrait(int portraitSpanCount) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        fishton.albumPortraitSpanCount = portraitSpanCount;
        return this;
    }

    @Override
    public FishBunCreator setAllViewTitle(String allViewTitle) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        fishton.titleAlbumAllView = allViewTitle;
        return this;
    }

    @Override
    public FishBunCreator setActionBarTitle(String actionBarTitle) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        fishton.titleActionBar = actionBarTitle;
        return this;
    }

    @Override
    public FishBunCreator setHomeAsUpIndicatorDrawable(Element icon) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        fishton.drawableHomeAsUpIndicator = icon;
        return this;
    }

    @Override
    public FishBunCreator setDoneButtonDrawable(Element icon) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        fishton.drawableDoneButton = icon;
        return this;
    }

    @Override
    public FishBunCreator setAllDoneButtonDrawable(Element icon) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        fishton.drawableAllDoneButton = icon;
        return this;
    }

    @Override
    public FishBunCreator setIsUseAllDoneButton(boolean isUse) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        fishton.isUseAllDoneButton = isUse;
        return this;
    }

    @Override
    public FishBunCreator exceptGif(boolean isExcept) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        if (isExcept) {
            this.fishton.exceptMimeTypeList = Arrays.asList(MimeType.GIF);
        }
        return this;
    }

    @Override
    public FishBunCreator exceptMimeType(List<MimeType> exceptMimeTypeList) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        fishton.exceptMimeTypeList = exceptMimeTypeList;
        return this;
    }

    @Override
    public FishBunCreator setMenuDoneText(String text) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        fishton.strDoneMenu = text;
        return this;
    }

    @Override
    public FishBunCreator setMenuAllDoneText(String text) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        fishton.strAllDoneMenu = text;
        return this;
    }

    @Override
    public FishBunCreator setMenuTextColor(int color) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        fishton.colorTextMenu = color;
        return this;
    }

    @Override
    public FishBunCreator setIsUseDetailView(boolean isUse) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        fishton.isUseDetailView = isUse;
        return this;
    }

    @Override
    public FishBunCreator setIsShowCount(boolean isShow) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        fishton.isShowCount = isShow;
        return this;
    }

    @Override
    public FishBunCreator setSelectCircleStrokeColor(int strokeColor) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        fishton.colorSelectCircleStroke = strokeColor;
        return this;
    }

    @Override
    public FishBunCreator isStartInAllView(boolean isStartInAllView) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        fishton.isStartInAllView = isStartInAllView;
        return this;
    }

    @Override
    public FishBunCreator setSpecifyFolderList(List<String> specifyFolderList) {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);
        fishton.specifyFolderList = specifyFolderList;
        return this;
    }

    public void startAlbum() {
//        FishBunCreator fishBunCreator = new FishBunCreator(fishBun, fishton);

        FishBun.FishBunContext fishBunContext = this.fishBun.getFishBunContext();
        Context context = fishBunContext.getContext();
        exceptionHandling();
        if (fishton.imageAdapter == null) {
            throw new NullPointerException("ImageAdapter is Null");
        }

        fishton.setDefaultMessage(context);
        fishton.setMenuTextColor();
        try {
            fishton.setDefaultDimen(context);
        } catch (NotExistException | WrongTypeException | IOException e) {
            e.printStackTrace();
        }

        Intent intent;
        if (fishton.isStartInAllView) {
            intent = PickerActivity.getPickerActivityIntent(context, 0L, this.fishton.titleAlbumAllView, 0);
        } else {
//            intent = new Intent(context, AlbumActivity.class);
            intent = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId("")
                    .withBundleName("com.harmonyos.fishbun")
                    .withAbilityName("com.sangcomz.fishbun.ui.album.ui.AlbumAbility")
                    .build();
            intent.setOperation(operation);
        }
        fishBunContext.startActivityForResult(intent, this.requestCode);
    }


    private void exceptionHandling() {
        //TODO support camera
        if (fishton.hasCameraInPickerPage) {
            fishton.hasCameraInPickerPage = fishton.specifyFolderList.isEmpty();
        }
    }
}

